<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="备货母单单号" prop="originalPurchaseOrderSn">
        <el-input
          v-model="queryParams.originalPurchaseOrderSn"
          placeholder="请输入备货母单单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家id" prop="supplierId">
        <el-input
          v-model="queryParams.supplierId"
          placeholder="请输入商家id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="spu_id" prop="spuId">
        <el-input
          v-model="queryParams.spuId"
          placeholder="请输入spu_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="skc_id" prop="skcId">
        <el-input
          v-model="queryParams.skcId"
          placeholder="请输入skc_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="skc图片" prop="skcPicture">
        <el-input
          v-model="queryParams.skcPicture"
          placeholder="请输入skc图片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间" prop="purchaseTime">
        <el-date-picker clearable
          v-model="queryParams.purchaseTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择下单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="发货时间" prop="deliverTime">
        <el-date-picker clearable
          v-model="queryParams.deliverTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="首单标记" prop="isFirst">
        <el-input
          v-model="queryParams.isFirst"
          placeholder="请输入首单标记"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['temu:sub_purchase_order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['temu:sub_purchase_order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['temu:sub_purchase_order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['temu:sub_purchase_order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sub_purchase_orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="备货单单号" align="center" prop="subPurchaseOrderSn" />
      <el-table-column label="备货母单单号" align="center" prop="originalPurchaseOrderSn" />
      <el-table-column label="商家id" align="center" prop="supplierId" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="spu_id" align="center" prop="spuId" />
      <el-table-column label="skc_id" align="center" prop="skcId" />
      <el-table-column label="skc图片" align="center" prop="skcPicture" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="下单时间" align="center" prop="purchaseTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.purchaseTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货时间" align="center" prop="deliverTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliverTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="首单标记" align="center" prop="isFirst" />
      <el-table-column label="紧急程度" align="center" prop="urgencyType" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['temu:sub_purchase_order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['temu:sub_purchase_order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改备货单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="备货母单单号" prop="originalPurchaseOrderSn">
          <el-input v-model="form.originalPurchaseOrderSn" placeholder="请输入备货母单单号" />
        </el-form-item>
        <el-form-item label="商家id" prop="supplierId">
          <el-input v-model="form.supplierId" placeholder="请输入商家id" />
        </el-form-item>
        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="spu_id" prop="spuId">
          <el-input v-model="form.spuId" placeholder="请输入spu_id" />
        </el-form-item>
        <el-form-item label="skc_id" prop="skcId">
          <el-input v-model="form.skcId" placeholder="请输入skc_id" />
        </el-form-item>
        <el-form-item label="skc图片" prop="skcPicture">
          <el-input v-model="form.skcPicture" placeholder="请输入skc图片" />
        </el-form-item>
        <el-form-item label="下单时间" prop="purchaseTime">
          <el-date-picker clearable
            v-model="form.purchaseTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择下单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货时间" prop="deliverTime">
          <el-date-picker clearable
            v-model="form.deliverTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="首单标记" prop="isFirst">
          <el-input v-model="form.isFirst" placeholder="请输入首单标记" />
        </el-form-item>
        <el-divider content-position="center">备货单sku信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTemuSubPurchaseOrderSku">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTemuSubPurchaseOrderSku">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="temuSubPurchaseOrderSkuList" :row-class-name="rowTemuSubPurchaseOrderSkuIndex" @selection-change="handleTemuSubPurchaseOrderSkuSelectionChange" ref="temuSubPurchaseOrderSku">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="货号" prop="extCode" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.extCode" placeholder="请输入货号" />
            </template>
          </el-table-column>
          <el-table-column label="商家价格" prop="supplierPrice" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.supplierPrice" placeholder="请输入商家价格" />
            </template>
          </el-table-column>
          <el-table-column label="下单数量" prop="purchaseQuantity" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.purchaseQuantity" placeholder="请输入下单数量" />
            </template>
          </el-table-column>
          <el-table-column label="送货数量" prop="deliverQuantity" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.deliverQuantity" placeholder="请输入送货数量" />
            </template>
          </el-table-column>
          <el-table-column label="入库数" prop="realReceiveAuthenticQuantity" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.realReceiveAuthenticQuantity" placeholder="请输入入库数" />
            </template>
          </el-table-column>
          <el-table-column label="图片" prop="thumbUrl" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.thumbUrl" placeholder="请输入图片" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSub_purchase_order, getSub_purchase_order, delSub_purchase_order, addSub_purchase_order, updateSub_purchase_order } from "@/api/temu/sub_purchase_order";

export default {
  name: "Sub_purchase_order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTemuSubPurchaseOrderSku: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 备货单表格数据
      sub_purchase_orderList: [],
      // 备货单sku信息表格数据
      temuSubPurchaseOrderSkuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        originalPurchaseOrderSn: null,
        supplierId: null,
        productName: null,
        spuId: null,
        skcId: null,
        skcPicture: null,
        status: null,
        purchaseTime: null,
        deliverTime: null,
        isFirst: null,
        urgencyType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询备货单列表 */
    getList() {
      this.loading = true;
      listSub_purchase_order(this.queryParams).then(response => {
        this.sub_purchase_orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        subPurchaseOrderSn: null,
        originalPurchaseOrderSn: null,
        supplierId: null,
        productName: null,
        spuId: null,
        skcId: null,
        skcPicture: null,
        status: null,
        purchaseTime: null,
        deliverTime: null,
        isFirst: null,
        urgencyType: null
      };
      this.temuSubPurchaseOrderSkuList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.subPurchaseOrderSn)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加备货单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const subPurchaseOrderSn = row.subPurchaseOrderSn || this.ids
      getSub_purchase_order(subPurchaseOrderSn).then(response => {
        this.form = response.data;
        this.temuSubPurchaseOrderSkuList = response.data.temuSubPurchaseOrderSkuList;
        this.open = true;
        this.title = "修改备货单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.temuSubPurchaseOrderSkuList = this.temuSubPurchaseOrderSkuList;
          if (this.form.subPurchaseOrderSn != null) {
            updateSub_purchase_order(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSub_purchase_order(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const subPurchaseOrderSns = row.subPurchaseOrderSn || this.ids;
      this.$modal.confirm('是否确认删除备货单编号为"' + subPurchaseOrderSns + '"的数据项？').then(function() {
        return delSub_purchase_order(subPurchaseOrderSns);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 备货单sku信息序号 */
    rowTemuSubPurchaseOrderSkuIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 备货单sku信息添加按钮操作 */
    handleAddTemuSubPurchaseOrderSku() {
      let obj = {};
      obj.extCode = "";
      obj.supplierPrice = "";
      obj.purchaseQuantity = "";
      obj.deliverQuantity = "";
      obj.realReceiveAuthenticQuantity = "";
      obj.thumbUrl = "";
      this.temuSubPurchaseOrderSkuList.push(obj);
    },
    /** 备货单sku信息删除按钮操作 */
    handleDeleteTemuSubPurchaseOrderSku() {
      if (this.checkedTemuSubPurchaseOrderSku.length == 0) {
        this.$modal.msgError("请先选择要删除的备货单sku信息数据");
      } else {
        const temuSubPurchaseOrderSkuList = this.temuSubPurchaseOrderSkuList;
        const checkedTemuSubPurchaseOrderSku = this.checkedTemuSubPurchaseOrderSku;
        this.temuSubPurchaseOrderSkuList = temuSubPurchaseOrderSkuList.filter(function(item) {
          return checkedTemuSubPurchaseOrderSku.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTemuSubPurchaseOrderSkuSelectionChange(selection) {
      this.checkedTemuSubPurchaseOrderSku = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('temu/sub_purchase_order/export', {
        ...this.queryParams
      }, `sub_purchase_order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
